var TableColConfig = function () {
  BaseModel.call(this);

  var allOrderInfoList = [{
    id: "companyOrderId",
    label: "订单编号"
  }, {
    id: "factoryId",
    label: "工厂型号"
  }, {
    id: "customerId",
    label: "客户型号"
  }, {
    id: "customerOrderId",
    label: "客户订单号"
  }, {
    id: "customer",
    label: "客户"
  }, {
    id: "jiaoDate",
    label: "交期"
  }, {
    id: "xiaDanDate",
    label: "下单日期"
  }, {
    id: "orderCount",
    label: "订单数"
  }, {
    id: "feedCount",
    label: "投料数"
  }];

  var self = this;
  var fn = {
    initData: function () {
      self.allTypeList = [];
      self.showTypeList = [];
      self.showTypeRes = null;

      self.tableHideColumns = null;
      self.orderInfoList = [];

      self.workshopList = [];
      self.deptObj = {};
    },

    setMaterialTypeConfig: function () {
      var allTypeList = self.allTypeList;
      var showTypeList = self.showTypeRes;
      var colData = [];
      if (showTypeList.length !== 0) {
        for (var i = 0; i < allTypeList.length; i++) {
          var type = allTypeList[i];
          if (showTypeList.indexOf(type) !== -1) {
            colData.push(type);
          }
        }
      }
      self.showTypeList = colData;
    },

    setOrderInfoConfig: function () {
      var tableHideColumns = self.tableHideColumns;
      var orderInfoList = [];
      for (var i = 0; i < allOrderInfoList.length; i++) {
        var obj = allOrderInfoList[i];
        var id = obj.id;
        if (tableHideColumns.indexOf(id) !== -1) {
          continue;
        }
        orderInfoList.push(id);
      }
      self.orderInfoList = orderInfoList;
    }
  };

  this.setMaterialTypeConfig = function (allTypeList, showTypeList) {
    var count = 0;
    if (allTypeList !== undefined) {
      if (!_.isEqual(this.allTypeList, allTypeList)) {
        this.allTypeList = allTypeList;
        count++;
      }
    }

    if (showTypeList !== undefined) {
      if (!_.isEqual(this.showTypeRes, showTypeList)) {
        this.showTypeRes = showTypeList;
        count++;
      }
    }

    if (count > 0) {
      fn.setMaterialTypeConfig();
    }
  };

  this.getMaterialTypeColConfig = function () {
    var allTypeList = this.allTypeList;
    var showTypeList = this.showTypeList;
    var tableTypeColList = [];
    var maxSize = 0;
    for (var i = 0; i < allTypeList.length; i++) {
      var type = allTypeList[i];

      var size = type.length;
      if (maxSize < size) {
        maxSize = size;
      }

      var checked = showTypeList.indexOf(type) !== -1;
      tableTypeColList.push({
        label: type,
        checked: checked
      });
    }
    return {
      tableTypeColList: tableTypeColList,
      maxSize: maxSize
    };
  };

  this.setOrderInfoConfig = function (tableHideColumns) {
    if (tableHideColumns !== undefined) {
      if (!_.isEqual(this.tableHideColumns, tableHideColumns)) {
        this.tableHideColumns = tableHideColumns;
        fn.setOrderInfoConfig();
      }
    }
  };

  this.getOrderInfoColConfig = function () {
    var orderInfoList = this.orderInfoList;
    var showList = [];
    var maxSize = 0;
    for (var i = 0; i < allOrderInfoList.length; i++) {
      var obj = allOrderInfoList[i];

      var label = obj.label;
      var size = label.length;
      if (maxSize < size) {
        maxSize = size;
      }

      var id = obj.id;
      var checked = orderInfoList.indexOf(id) !== -1;
      showList.push({
        id: id,
        label: obj.label,
        checked: checked
      });
    }
    return {
      showList: showList,
      maxSize: maxSize
    };
  };

  this.setWorkshopConfig = function (workshopList) {
    var workshopGroup = _.groupBy(workshopList, function (obj) {
      var sort = obj.sort;
      if (sort === null) {
        sort = 0;
      }
      return sort;
    });

    var workshopNameList = [];
    for (var sort in workshopGroup) {
      var list = workshopGroup[sort];
      if (list.length > 1) {
        workshopNameList.push({
          name: `车间${sort}`,
          sort: sort
        });
      } else {
        workshopNameList.push({
          name: list[0].workshopName,
          sort: sort
        });
      }
    }
    this.workshopList = workshopNameList;

    var deptObj = {};
    for (var i = 0; i < workshopList.length; i++) {
      var obj = workshopList[i];
      var deptId = obj.deptId;
      if (deptObj[deptId] === undefined) {
        deptObj[deptId] = obj.productionLine;
      }
    }
    this.deptObj = deptObj;
  };

  fn.initData();
};

var MaterialMatchInfoSearch = function () {
  BaseModel.call(this);

  var self = this;
  var propertyList = ['companyOrderId', 'factoryId', 'customerId', 'customerOrderId', 'customer', 'jiaoDateStart', 'jiaoDateEnd', 'xiaDanDateStart',
    'xiaDanDateEnd', 'orderDept'
  ];
  var fn = {
    initData: function () {
      self.companyOrderId = '';
      self.factoryId = '';
      self.customerId = '';
      self.customerOrderId = '';
      self.customer = '';
      self.jiaoDateStart = null;
      self.jiaoDateEnd = null;
      self.xiaDanDateStart = null;
      self.xiaDanDateEnd = null;
      self.orderDept = '';
      self.saveOldData();
    }
  };

  this.saveOldData = function () {
    this.setOldData(propertyList);
  };

  this.resetOldData = function () {
    for (var i = 0; i < propertyList.length; i++) {
      var property = propertyList[i];
      var value = this.getOldData(property);
      value = _.cloneDeep(value);
      this[property] = value;
    }
  };

  this.filterData = function (orderMatch) {
    var fields = ['companyOrderId', 'factoryId', 'customerId', 'customerOrderId', 'customer'];
    for (var i = 0; i < fields.length; i++) {
      var field = fields[i];
      var value = this[field];
      if (value === '') {
        continue;
      }

      value = value.toLowerCase();
      var orderValue = orderMatch[field];
      orderValue = orderValue.toLowerCase();
      if (orderValue.indexOf(value) === -1) {
        return false;
      }
    }

    var orderDept = this.orderDept;
    if (orderDept !== '') {
      var deptId = orderDept.deptId;
      if (deptId !== orderMatch.deptId) {
        return false;
      }
    }

    var dateFields = ['jiaoDate', 'xiaDanDate'];
    for (var i = 0; i < dateFields.length; i++) {
      var dateField = dateFields[i];
      var time = orderMatch[dateField];
      var startTime = this[`${dateField}Start`];
      if (startTime !== null) {
        if (time === '') {
          return false;
        }
        if (moment(time).isBefore(startTime, 'day')) {
          return false;
        }
      }

      var endTime = this[`${dateField}End`];
      if (endTime !== null) {
        if (time === '') {
          return false;
        }
        if (moment(time).isAfter(endTime, 'day')) {
          return false;
        }
      }
    }
    return true;
  };

  fn.initData();
};

var MaterialMatchInfo = function (orderMatch, deptObj) {
  BaseModel.call(this);

  var searchOrderFields = ['companyOrderId', 'factoryId', 'customerId', 'customerOrderId', 'customer', 'deptId', 'deptName'];
  var self = this;
  var fn = {
    initData: function (orderMatch, deptObj) {
      var fields = ['companyOrderId', 'factoryId', 'customerId', 'customerOrderId', 'customer', 'orerId', 'orderCount', 'feedCount'];
      self.setDataFromObj(orderMatch, fields, '');

      var jiaoDate = orderMatch.jiaoDate;
      if (SysUtil.isEmpty(jiaoDate)) {
        jiaoDate = '';
      } else {
        jiaoDate = moment(jiaoDate, 'YYYY/MM/DD').valueOf();
      }
      self.jiaoDate = jiaoDate;

      var xiaDanDate = orderMatch.xiaDanDate;
      if (SysUtil.isEmpty(xiaDanDate)) {
        xiaDanDate = '';
      } else {
        xiaDanDate = moment(xiaDanDate, 'YYYY/MM/DD').valueOf();
      }
      self.xiaDanDate = xiaDanDate;

      var deptId = orderMatch.deptId;
      var deptName = orderMatch.deptName;
      if (deptName === null) {
        deptName = deptObj[deptId];
        if (SysUtil.isEmpty(deptName)) {
          deptName = '';
        }
      }
      self.deptId = deptId;
      self.deptName = deptName;

      this.setWorkshopData(orderMatch);
      this.setMaterialTypeData(orderMatch);
    },

    setWorkshopData: function (orderMatch) {
      var wpcbList = orderMatch.wpcbList;
      self.wpcbList = wpcbList;

      var wpcbGroup = _.groupBy(wpcbList, function (obj) {
        var sort = obj.sort;
        if (SysUtil.isEmpty(sort)) {
          return 0;
        }
        return sort;
      });

      var workshopData = {};
      for (var sort in wpcbGroup) {
        var data = wpcbGroup[sort];
        var maixStatus;
        if (data.length > 1) {
          var obj = _.find(data, {
            maixStatus: 2
          });
          maixStatus = obj ? 2 : 4;
        } else {
          maixStatus = data[0].maixStatus;
        }
        workshopData[sort] = maixStatus;
      }
      self.workshopData = workshopData;
    },

    setMaterialTypeData: function (orderMatch) {
      var orderMaterialList = orderMatch.orderMaterialList;
      if (SysUtil.isEmpty(orderMaterialList)) {
        orderMaterialList = [];
      }
      var otherOrderMaterialList = [];
      var commonOrderMaterialList = [];
      var materialTypeData = {};
      for (var i = 0; i < orderMaterialList.length; i++) {
        var obj = orderMaterialList[i];
        var lineNo = obj.lineNo;
        if (lineNo === '-1') {
          otherOrderMaterialList.push(obj);
          this.setStockCount(materialTypeData, obj);
        } else {
          commonOrderMaterialList.push(obj);
        }
      }

      var matchGroup = _.groupBy(commonOrderMaterialList, 'materialType');
      for (var materialType in matchGroup) {
        var list = matchGroup[materialType];
        var stockCount = _.sumBy(list, 'stockCount');
        var recevieCount = _.sumBy(list, function (obj) {
          return obj.recevieCount ? obj.recevieCount : 0;
        });
        if (stockCount === 0 && recevieCount === 0) {
          stockCount = null;
        }
        materialTypeData[materialType] = stockCount;
      }

      self.materialTypeData = materialTypeData;
      self.otherOrderMaterialList = otherOrderMaterialList;
      self.commonOrderMaterialList = commonOrderMaterialList;
    },

    setStockCount: function (materialTypeData, orderMaterial) {
      var stockCount = orderMaterial.stockCount;
      var recevieCount = orderMaterial.recevieCount;
      if (stockCount === 0) {
        if (SysUtil.isEmpty(recevieCount) || recevieCount === 0) {
          stockCount = null;
        }
      }

      var materialType = orderMaterial.materialType;
      materialTypeData[materialType] = stockCount;
    },

    calStockCount: function (stockCount, materialTypeList) {
      if (stockCount === 0) {
        return [];
      }
      var sumCount = _.sumBy(materialTypeList, 'stockCount');
      if (sumCount === 0) {
        return [];
      }
      var count = 0;
      var stockCountArray = [];
      for (var i = 0; i < materialTypeList.length; i++) {
        var obj = materialTypeList[i];
        var materialCount = obj.stockCount;
        materialCount = stockCount / sumCount * materialCount;
        materialCount = _.floor(materialCount, 2);
        stockCountArray.push(materialCount);
        count += materialCount;
      }

      var surplusCount = _.round(sumCount - count, 2);
      if (surplusCount > 0) {
        for (var i = 0; i < stockCountArray.length; i++) {
          var stockCount = stockCountArray[i];
          stockCountArray[i] = stockCount + 0.01;
          surplusCount -= 0.01;
          if (surplusCount === 0) {
            break;
          }
        }
      }
      return stockCountArray;
    }
  };

  this.getSearchOrder = function () {
    var obj = {};
    for (var i = 0; i < searchOrderFields.length; i++) {
      var field = searchOrderFields[i];
      var value = self[field];
      obj[field] = value;
    }
    return obj;
  };

  this.getMaterialTypeList = function () {
    var commonOrderMaterialList = this.commonOrderMaterialList;
    commonOrderMaterialList = _.orderBy(commonOrderMaterialList, [function (obj) {
      var lineNo = obj.lineNo;
      return Number(lineNo);
    }], ['asc']);
    var materialGroup = _.groupBy(commonOrderMaterialList, 'lineNo');
    var materialTypeList = [];
    for (var lineNo in materialGroup) {
      var list = materialGroup[lineNo];
      materialTypeList.push({
        lineNo: lineNo,
        stockCount: list[0].stockCount,
        list: list,
        otherList: []
      });
    }

    var otherOrderMaterialList = this.otherOrderMaterialList;
    for (var i = 0; i < otherOrderMaterialList.length; i++) {
      var otherOrderMaterial = otherOrderMaterialList[i];
      var stockCount = otherOrderMaterial.stockCount;
      var countArray = fn.calStockCount(stockCount, materialTypeList);
      for (var j = 0; j < materialTypeList.length; j++) {
        var count = countArray[j];
        if (count === undefined) {
          count = 0;
        }
        materialTypeList[j].otherList.push({
          materialType: otherOrderMaterial.materialType,
          stockCount: count
        });
      }
    }

    var materialTypeData = [];
    for (var i = 0; i < materialTypeList.length; i++) {
      var data = materialTypeList[i];
      var list = data.list;
      var lineNo = data.lineNo;
      for (var j = 0; j < list.length; j++) {
        var obj = list[j];
        materialTypeData.push({
          lineNo: lineNo,
          materialType: obj.materialType,
          stockCount: obj.stockCount
        });
      }

      var otherList = data.otherList;
      for (var j = 0; j < otherList.length; j++) {
        var otherObj = otherList[j];
        materialTypeData.push({
          lineNo: lineNo,
          materialType: otherObj.materialType,
          stockCount: otherObj.stockCount
        });
      }
    }

    return materialTypeData;
  };

  fn.initData(orderMatch, deptObj);
};
